package com.cysion.shell

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.cysion.otbox.Box
import com.cysion.otbox.base.BaseModelActivity
import com.cysion.otbox.ext.*
import com.cysion.otbox.utils.*
import com.cysion.shell.test.A
import com.cysion.shell.vm.MainViewModel
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.tencent.mmkv.MMKV
import java.io.File

class MainActivity : BaseModelActivity<MainViewModel>() {

    fun files() {
        val fname = "hello_world"
        launchUI {
            val pathOfSpecImg = pathOfSpecImg(fname)
            val f = File(pathOfSpecImg)
            if (!f.exists()) {
                f.createNewFile()
            }
            logd(f.absolutePath)
            logd(pathOfSpecSound(fname))
            logd(pathOfSpecVideo(fname))
            logd(pathOfExtVideo(fname))


            val pathOfExtImg = pathOfExtImg(fname)
            val file = File(pathOfExtImg)
            if (!file.exists()) {
                file.createNewFile()
            }
            logd(file.absolutePath)

            val pathOfExtDownload = pathOfExtDownload(fname)
            val f2 = File(pathOfExtDownload)
            if (!f2.exists()) {
                f2.createNewFile()
            }
            logd(f2.absolutePath)
        }
    }

    override fun getLayoutId(): Int =R.layout.activity_main

    override fun initView() {
        Box.init(this, true)
        MMKV.initialize(this)
        XXPermissions.with(this)
            .permission(Permission.WRITE_EXTERNAL_STORAGE)
            .permission(Permission.CAMERA)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    if (all) {
                        files()
                    } else {
                        MyToast.toast(this@MainActivity, "部分权限未能获取")
                    }
                }

                override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                    if (never) {
                        MyToast.toast(Box.ctx(), "请手动设置")
                        XXPermissions.startPermissionActivity(this@MainActivity,Permission.WRITE_EXTERNAL_STORAGE)
                    } else {
                        MyToast.toast(Box.ctx(), "获取权限失败")
                    }
                }
            })
        viewModel.getCodeMsg()
        viewModel.getErrMsg()
        viewModel.getStaMsg()
        val str = "51"
        val i = 51
        val res=str.equals(i)
        res.toString()
        res.toString()
    }

    override fun observeModel() {

    }

    override fun onReceivedStateEvent(code: String, msg: String) {
        MyToast.toast(this,msg)

    }
}